package com.example.safeassistan.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.home.adapter.TaskCheckTypeListAdapter;
import com.example.safeassistan.home.model.TaskCheckTypeModel;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.utile.JsonUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class ActivityCheckTypeList extends BaseActivity implements View.OnClickListener {
    private TaskCheckTypeListAdapter adapter;
    private List<TaskCheckTypeModel.ResultDTO> data;
    private LinearLayout llNodata;
    private LinearLayout llTop;
    private SmartRefreshLayout refresh;
    private RecyclerView rcView;
    private String defId="";

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, ActivityCheckTypeList.class);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.riskdetail_checklog_fragment;
    }

    @Override
    protected void initView() {
        setTitleText("检查类型");
        llNodata=findViewById(R.id.llNodata);
        llTop=findViewById(R.id.llTop);
        llTop.setVisibility(View.GONE);
        refresh=findViewById(R.id.refresh);
        rcView=findViewById(R.id.rcView);
        getData();
    }

    //获取数据
    public void getData(){

        new HttpRequest(ActivityCheckTypeList.this).doPost(UrlConstants.taskgetAllCheckType, null, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), TaskCheckTypeModel.class);
                        if (obj instanceof TaskCheckTypeModel) {
                            TaskCheckTypeModel model=(TaskCheckTypeModel) obj;
                                if (model.getResult().size() > 0) {
                                    llNodata.setVisibility(View.GONE);
                                    rcView.setVisibility(View.VISIBLE);
                                    data=model.getResult();
                                    adapter=new TaskCheckTypeListAdapter(data);
                                    rcView.setLayoutManager(new LinearLayoutManager(ActivityCheckTypeList.this));
                                    rcView.setAdapter(adapter);
                                    adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                        @Override
                                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                            Intent intent = new Intent();
                                            intent.putExtra("id",model.getResult().get(position).getId());
                                            intent.putExtra("name",model.getResult().get(position).getCheckTypeName());
                                            setResult(RESULT_OK, intent);
                                            finish();
                                        }
                                    });
                                } else {
                                    llNodata.setVisibility(View.VISIBLE);
                                    refresh.finishRefreshWithNoMoreData();
                                }
                        }
                    }else {
                        Toast.makeText(ActivityCheckTypeList.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                refresh.finishLoadMore();
                refresh.finishRefresh();
            }
        });
    }

    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return 0;
    }
}